
struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
  ListNode *removeElements(ListNode *head, int val) {
    if (head == nullptr)
      return nullptr;
    ListNode *p = head;
    ListNode *t = head->next;
    while (t != nullptr) {
      if (t->val == val) {
        p->next = t->next;
      } else {
        p = p->next;
      }
      t = t->next;
    }
    if (head->val == val) {
      return head->next;
    }
    return head;
  }
};

int main(int argc, char *argv[]) { return 0; }
